package qianmo.system.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import qianmo.common.log.annotation.Log;
import qianmo.common.log.enums.BusinessType;
import qianmo.common.security.annotation.RequiresPermissions;
import qianmo.system.domain.QuestionPracticeRecord;
import qianmo.system.service.IQuestionPracticeRecordService;
import qianmo.common.core.web.controller.BaseController;
import qianmo.common.core.web.domain.AjaxResult;
import qianmo.common.core.utils.poi.ExcelUtil;
import qianmo.common.core.web.page.TableDataInfo;

/**
 * 题目练习记录Controller
 * 
 * @author qianmo
 * @date 2025-03-23
 */
@RestController
@RequestMapping("/record")
public class QuestionPracticeRecordController extends BaseController
{
    @Autowired
    private IQuestionPracticeRecordService questionPracticeRecordService;

    /**
     * 查询题目练习记录列表
     */
    @RequiresPermissions("system:record:list")
    @GetMapping("/list")
    public TableDataInfo list(QuestionPracticeRecord questionPracticeRecord)
    {
        startPage();
        List<QuestionPracticeRecord> list = questionPracticeRecordService.selectQuestionPracticeRecordList(questionPracticeRecord);
        return getDataTable(list);
    }

    /**
     * 导出题目练习记录列表
     */
    @RequiresPermissions("system:record:export")
    @Log(title = "题目练习记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, QuestionPracticeRecord questionPracticeRecord)
    {
        List<QuestionPracticeRecord> list = questionPracticeRecordService.selectQuestionPracticeRecordList(questionPracticeRecord);
        ExcelUtil<QuestionPracticeRecord> util = new ExcelUtil<QuestionPracticeRecord>(QuestionPracticeRecord.class);
        util.exportExcel(response, list, "题目练习记录数据");
    }

    /**
     * 获取题目练习记录详细信息
     */
    @RequiresPermissions("system:record:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(questionPracticeRecordService.selectQuestionPracticeRecordById(id));
    }

    /**
     * 新增题目练习记录
     */
    @RequiresPermissions("system:record:add")
    @Log(title = "题目练习记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody QuestionPracticeRecord questionPracticeRecord)
    {
        return toAjax(questionPracticeRecordService.insertQuestionPracticeRecord(questionPracticeRecord));
    }

    /**
     * 修改题目练习记录
     */
    @RequiresPermissions("system:record:edit")
    @Log(title = "题目练习记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody QuestionPracticeRecord questionPracticeRecord)
    {
        return toAjax(questionPracticeRecordService.updateQuestionPracticeRecord(questionPracticeRecord));
    }

    /**
     * 删除题目练习记录
     */
    @RequiresPermissions("system:record:remove")
    @Log(title = "题目练习记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(questionPracticeRecordService.deleteQuestionPracticeRecordByIds(ids));
    }

    @RequiresPermissions("system:record:findByStudentId")
    @GetMapping("/question/{questionId}/student/{studentId}")
    public AjaxResult getRecordsByQuestionAndStudent(
            @PathVariable Long questionId, @PathVariable Long studentId) {
        List<QuestionPracticeRecord> records =
                questionPracticeRecordService.getRecordsByQuestionAndStudent(questionId, studentId);
        return success(records);
    }
}
